let params = this.getVariables().find(v => v.name === "screen_params")?.value;
const prefix = this.getVariables().find(v => v.name === "environmentType")?.value;
const search = this.getApiWidgetByAliasName('search')?.widgetObj;
const eventTitle = this.getApiWidgetByAliasName('eventTitle')?.widgetObj;
const list = this.getApiWidgetByAliasName('list')?.widgetObj;

try {
  params = JSON.parse(params);
} catch (error) {
  console.error(error)
}

function baseFetch(url, params) {
  return fetch(url, {
    method: 'POST',
    headers: { "Content-type": "application/json" },
    body: JSON.stringify(params),
  }).then(res => res.json())
}

console.log('生命周期', params);

if(params) {
  const options = { eventType: params.eventTypeCode, eventLevel: params.eventGradeCode };
  eventTitle._option.links.text = params.eventTitle;
  eventTitle.render();
  list.setEvent(params);
  baseFetch(`/${prefix}SummaryAssessmentSV/emergencyHandle/queryEmergencyPlan`, options).then(({ success, result }) => {
    if(success) {
      const arr = result?.map((v, i) => ({
        ...v,
        name: v.planName + (i === 0 ? '(推荐)' : ''),
        value: v.id
      }));
      // list.setParams(arr[0].id, params.eventGradeCode)
      list.setParams(arr[0].id, '1692442354007216130');
      // console.log('name+++++', arr)
      search.asyncSelectOptions('name', arr);
      search.setFormData('form_1', {
        name: arr[0].id,
        level: params.eventGradeCode
      });

      list.initQuery()
    }
  })
}
